import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Put } from '@nestjs/common';
import { RolesService } from './roles.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { RoleQueryDto } from './dto/query-role.dto';
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { AssignMenuDto } from '../menu/dto/assign-menu.dto';

@ApiTags('admin-角色管理')
@Controller('roles')
export class RolesController {
  constructor(private readonly rolesService: RolesService) {}

  @Post()
  @ApiOperation({ summary: '创建角色' })
  create(@Body() createRoleDto: CreateRoleDto) {
    return this.rolesService.create(createRoleDto);
  }

  @Get()
  @ApiOperation({ summary: '查询角色列表' })
  findAll(@Query() query: RoleQueryDto) {
    return this.rolesService.findAll(query);
  }

  // 先定义固定路径的路由
  @Get('options')
  @ApiOperation({ summary: '获取角色下拉选择列表' })
  getRoleOptions() {
    return this.rolesService.getRoleOptions();
  }

  // 然后定义带参数的路由和其相关操作
  @Get(':id')
  @ApiOperation({ summary: '获取角色详情' })
  @ApiParam({ name: 'id', description: '角色ID' })
  findOne(@Param('id') id: number) {
    return this.rolesService.findOne(id);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新角色' })
  update(@Param('id') id: number, @Body() updateRoleDto: UpdateRoleDto) {
    return this.rolesService.update(id, updateRoleDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除角色' })
  @ApiParam({ name: 'id', description: '角色ID' })
  remove(@Param('id') id: number) {
    return this.rolesService.remove(id);
  }

  @Post(':id/menus')
  @ApiOperation({ summary: '为角色分配菜单权限' })
  @ApiParam({ name: 'id', description: '角色ID' })
  assignMenus(
    @Param('id') id: number,
    @Body() assignMenuDto: AssignMenuDto
  ) {
    return this.rolesService.assignMenus(id, assignMenuDto);
  }

  @Get(':id/menusList')
  @ApiOperation({ summary: '获取角色的菜单权限列表' })
  @ApiParam({ name: 'id', description: '角色ID' })
  async getRoleMenus(@Param('id') id: number) {
    return this.rolesService.getRoleMenus(id);
  }
}
